package com.grep.extra.factory.handle.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.grep.base.cache.WebSitePathCache;
import com.grep.base.util.FilePathUtil;
import com.grep.base.util.PropertiesUtil;
import com.grep.extra.dao.ContentUrlDao;
import com.grep.extra.dao.impl.ContentUrlDAOSQL;
import com.grep.extra.dao.impl.TaskDAOSQL;
import com.grep.extra.factory.data.IDataQueue;
import com.grep.extra.factory.data.impl.TaskContentUrl;
import com.grep.extra.factory.data.impl.TaskUrl;
import com.grep.extra.factory.handle.IHandler;
import com.grep.extra.factory.utils.JsoupUtil;

public class TaskUrlHandler implements IHandler<TaskUrl,TaskContentUrl> {
	
	static Logger logger = LoggerFactory.getLogger(TaskUrlHandler.class);    
	
	private IDataQueue<TaskUrl> task = null ;
	
	private IDataQueue<TaskContentUrl> data = null ;
	
	private TaskDAOSQL taskDao = new TaskDAOSQL() ;
	
	private ContentUrlDao contentUrlDao = new ContentUrlDAOSQL();
	
	//private String siteid = "";
	private String taskid = "";
	
	//空构造方法
	public TaskUrlHandler(){}
	
//	public TaskUrlHandler(String siteid){
//		this.siteid = siteid;
//	}
	//传入taskID的构造方法
	public TaskUrlHandler(String taskid){
		this.taskid = taskid;

	}
	
	
	public IDataQueue<TaskUrl> getTask() {
		return task;
	}
	/**
	 * 读取所有任务
	 */
	private void initTask(){
		List<TaskUrl> list = taskDao.getTaskUrls() ;//new ArrayList<TaskUrl> () ;
		//List<TaskUrl> list = taskDao.getTaskUrlsBySiteId(siteid);
		//List<TaskUrl> list = taskDao.getTaskUrlsByTaskId(taskid);
		task.putAll(list);
	}
	/**
	 * 初始化流入和流出数据集
	 */
	public void init(IDataQueue<TaskUrl> task,IDataQueue<TaskContentUrl> data) {
		this.task = task ;
		this.data = data ;
		//this.initTask();		
	}
 
	/**
	 * 处理一个任务下的所有的内容页。将所有的内容页下载保存到本地,保存的路径是
	 * 配制文件中配制的根路径 + 网站根目录 + 任务文件夹目录
	 * 但是如果每次，通过任务去数据库查网站根目录，着实不行，所以，网站根目录保存在内存中
	 * 调用缓存方法
	 */
	public List<TaskContentUrl> process(TaskUrl taskUrl) {
		logger.debug("[TaskUrlHandler] handler start .") ;
		String filePath = PropertiesUtil.getInstance().geRootPath() +"//"+WebSitePathCache.load(taskUrl.getTaskId())+"//"+taskUrl.getTaskEnName();
		taskUrl.setPath(filePath) ;
		saveContentUrls(getContentUrls(taskUrl),taskUrl) ;
		logger.debug("[TaskUrlHandler] handler end .") ;
		return null;
	}

	/**
	 * 保存内容URL
	 * @param list
	 */
	private void saveContentUrls(List<String> list ,TaskUrl taskUrl){
		for(String str :list){
			str = str.trim();
			TaskContentUrl contentUrl = new TaskContentUrl() ;
			contentUrl.setTaskContentUrl(generateUrl(str,taskUrl));
			contentUrl.setPath(taskUrl.getPath()) ;
			contentUrl.setTaskUrlId(taskUrl.getId()) ;
			contentUrl.setTaskId(taskUrl.getTaskId());
			List<TaskContentUrl> old = contentUrlDao.getContentUrlByUrl(
					contentUrl.getTaskContentUrl()) ;
			if(old != null && old.size() < 1){
				contentUrlDao.insertContentUrl(contentUrl) ;
			}else {
				contentUrl.setId(old.get(0).getId()) ;
			}
			data.put(contentUrl);
		}
	}
	/**
	 * 生产全路径的URL
	 * @param str
	 * @param taskUrl
	 * @return
	 */
	private String generateUrl (String str ,TaskUrl taskUrl){
		if(str!=null && !str.trim().startsWith("http") && taskUrl.getSiteUrl()!= null){
			String site = taskUrl.getSiteUrl().getSiteUrl();
			if(site != null ){
				site = site.trim();
				if(str.startsWith("/")){
					if(site.endsWith("/")){
						str = site + str.substring(1);
					}else{
						str = site + str;
					}
				}else{
					if(site.endsWith("/")){
						str = site + str;
					}else{
						str = site + "/" + str;
					}
				}
			}
		}
		return str ;
	}
	/**
	 * 更加URL和特定的解析路径提取页面中的链接地址
	 * @param taskUrl
	 * @return
	 */
	private List<String> getContentUrls(TaskUrl taskUrl){
		String url = taskUrl.getTaskUrl() ;
		FilePathUtil.mkdir(taskUrl.getPath()) ;
		String analyse = taskUrl.getTaskAnalyse() ;
		if(url == null){
			logger.error("[TaskUrlHandler] task url is null.") ;
			return null;
		}
		if(analyse == null){
			logger.error("[TaskUrlHandler] task analyse is null." );
			return null;
		}
		logger.debug("[TaskUrlHandler] task url is:{} .",taskUrl.getTaskUrl()) ;
		JsoupUtil jsoup = new JsoupUtil(url) ;
		return jsoup.getURLSByPath(analyse);
	}
}
